<?php
$JO["subject"		] = JO(array("EN"=>"Subject",		"VI"=>"Tiêu Đề"));
$JO["description"	] = JO(array("EN"=>"Description",	"VI"=>"Mô Tả"));
$JO["viewer"		] = JO(array("EN"=>"Viewer",		"VI"=>"Lượt Xem"));
$JO["episode"		] = JO(array("EN"=>"Episode",		"VI"=>"Tập"));
$JO["image"			] = JO(array("EN"=>"image",			"VI"=>"Hình Ảnh"));
$JO["part"			] = JO(array("EN"=>"Part",			"VI"=>"Phần"));
$JO["url"			] = JO(array("EN"=>"URL",			"VI"=>"URL"));
$JO["delete"		] = JO(array("EN"=>"Delete",		"VI"=>"Xóa"));
$JO["show"			] = JO(array("EN"=>"Show Name",		"VI"=>"Chương Trình"));

$JO["insert"		] = JO(array(	"EN"=>"Insert A Show",
									"VI"=>"Thêm Chương Trình"));

$JO["update"		] = JO(array(	"EN"=>"Update A Show",
									"VI"=>"Cập Nhật Chương Trình"));

$JO["upload_file"]	= JO(array(	"EN"=>"Upload file to server!",
								"VI"=>"Đính kèm hình ảnh từ máy"));

$JO["attach_link"]	= JO(array(	"EN"=>"Attach link of image!",
								"VI"=>"Đính kèm liên kết của hình ảnh"));

$JO["add_field"		] = JO(array(	"EN"=>"Add More Input Field(s)!",
									"VI"=>"Thêm Trường Nhập Liệu"));
?>

<!-- script add part of show -->
<script type="text/javascript">
<!-- Begin
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Husay :: http://www.communitxt.net */

var arrInput = new Array(0);
var arr_subject = new Array(0);
var arr_part = new Array(0);
var arr_url = new Array(0);
var arr_desc = new Array(0);

function addInput() {
  //arrInput.push(createInput(arrInput.length));
  arrInput.push(arrInput.length);
  //arrInputValue.push(arrInputValue.length);
  arr_subject.push("");
  arr_part.push("");
  arr_url.push("");
  arr_desc.push("");
  display();
}

function display() {
  document.getElementById('tb_detail').innerHTML="";
  document.getElementById('SO_show_array').value = arrInput.length;
  var ct = "";
  ct +="<tr>";
  //ct +="<th></th>";
  ct +="<th style='width: 28px;'><?=$JO["part"];?>		</th>";
  ct +="<th>					<?=$JO["url"];?>		</th>";
  ct +="<th>					<?=$JO["description"];?></th>";
  ct +="<th style='width: 28px;vertical-align: middle;text-align: center;'><?=$JO["delete"];?></th>";
  ct +="</tr>";
  document.getElementById('tb_detail').innerHTML+=ct;
  var intI = 0;
  for (intI=0;intI<arrInput.length;intI++) {
	  document.getElementById('tb_detail').innerHTML+=createInput(intI,arrInput[intI], arr_subject[intI],intI+1,arr_url[intI],arr_desc[intI]);
  }
  ct = "";
  ct +="<tr>";
  ct +="<td colspan='4'>";
  ct +="	<a href='javascript:addInput()'><?=$JO["add_field"];?></a><br>";
  ct +="</td>";
  ct +="</tr>";
  document.getElementById('tb_detail').innerHTML+=ct;
}

function saveSubject(intId,strValue) {
	arr_subject[intId]=strValue;
}

function savePart(intId,strValue) {
	arr_part[intId]=strValue;
}

function saveUrl(intId,strValue) {
	arr_url[intId]= strValue;
}

function saveDesc(intId,strValue) {
	arr_desc[intId]=strValue;
}

function createInput(id,idte,subject,part,url,mota) {
	var ct = "";
	ct +="<tr>";
	//ct +="<td><input type='text' name='txt_subject"+ id +"'	id='txt_subject"+ id +"'	style='width:98%;' onChange='javascript:saveSubject("+ id +",this.value)' value='"+ subject +"'/>	</td>";
	ct +="<td><input type='text' name='txt_part"+ id +"'	id='txt_part"+ id +"'		style='width:92%;' onChange='javascript:savePart("+ id +",this.value)' value='"+ part +"' onkeypress='return keypress(event);'/>	</td>";
	ct +="<td><input type='text' name='txt_url"+ id +"'		id='txt_url"+ id +"'		style='width:99%;' onChange='javascript:saveUrl("+ id +",this.value)' value='"+ url +"'/>	</td>";
	ct +="<td><input type='text' name='txt_mota"+ id +"'	id='txt_mota"+ id +"'		style='width:100%;' onChange='javascript:saveDesc("+ id +",this.value)' value='"+ mota +"'/></td>";
	ct +="<td style='vertical-align: middle;text-align: center;'><a href='#' onclick='deleteInput("+id+")'>";
	ct +="<img src='JO_80_image/JO_23_icon_delete_off.png' class='JO_icon' onmouseover= \"this.src='JO_80_image/JO_23_icon_delete_on.png';\"	onmouseout=\"this.src='JO_80_image/JO_23_icon_delete_off.png';\"/>";
	ct +="</a></td>";
	ct +="</tr>";
	return ct; 
}

function deleteInput(index) {
  if (arrInput.length > 0) {
     arrInput.splice(index,1);
     arr_subject.splice(index,1);
     arr_part.splice(index,1);
     arr_url.splice(index,1);
     arr_desc.splice(index,1);
  }
  display();
}
// End-->

function ShowProduct(nid){
	location=('JO_00_integration.php?SO_unit=13_show&SO_show_key='+nid);
}

////function for keypress
//You only put numeric data into text box 
//letter and character is not allowed
function keypress(e)
{
	var keypressed = null;
	if (window.event)
	{
		keypressed = window.event.keyCode; //IE
	}
	else 
	{
		keypressed = e.which; //NON-IE, Standard
	}
	if (keypressed < 48 || keypressed > 57)
	{
		if (keypressed == 8 || keypressed == 127)
		{
		return;
		}
		return false;
	}
}

function checkInput(param)
{  
	if (param.txt_subject.value=="")
	{
		alert("Xin hãy điền vào tên chương trình ti-vi!");
		param.txt_subject.focus();
		return false;
	}

	if (param.txt_episode.value=="")
	{
		alert("Xin hãy điền vào tập phim!");
		param.txt_episode.focus();
		return false;
	}

	var l = param.txt_episode.value;
	if (l.length>=4){
		alert("Số tập không quá 999");//your amounts do not exceed 99 million
		param.txt_episode.focus();
		return false;
	}
	if (l<1){
		alert("Số tập không thấp hơn 1");//your amounts do not exceed 99 million
		param.txt_episode.focus();
		return false;
	}

	return true;
}

function delete_items(){
	var chk=document.frm_show.chk_delete;
	var sel = 0;
    var i;
    var or=" ";
    var wherequery="";
    if(!Number(chk.length)){
        if(!chk.checked){ alert("Bạn phải chọn ít nhất 1 mục!");return; }
        wherequery+=or+"`show`.`key`="+chk.value;
    }else{
	    for (i = 0;  i < chk.length;  i++){ if (chk[i].checked==true) sel++; }
	        if (sel ==0) { alert("Bạn phải chọn ít nhất 1 mục!");return; }
	    var i;
	    for(i=0;i<chk.length;i++){
	        if(chk[i].checked){
	            wherequery+=or+"`show`.`key`="+chk[i].value;
	            or=" or ";
	        };
	    };
    }
    
    if(confirm("Bạn muốn xóa các mục đã chọn?")){
        document.getElementById( "JO_integration_asynchronous" ).src = "JO_71_SO/SO_05_execution.php?SO_query=show_delete&wherequery="+wherequery;
    }
}
</script>
<!-- Add List of detail of selected product into array by javascrip, then use function display to show them-->
<?php if(isset($_GET["SO_show_key"])){
	$querytemp = "select * from `show_detail` where `show`=".$_GET["SO_show_key"]." ORDER BY `part` ASC";
	$recordSettemp = JO_query( $querytemp );
	$i=0;
	if( $recordSettemp -> num_rows )
	while( $recordtemp = $recordSettemp -> fetch_array( ) ) {
		echo "<script>
				arrInput.push(arrInput.length);
				arr_subject.push('');
				arr_part.push('');
				arr_url.push('');
				arr_desc.push('');
				arr_part[".$i."]='".$recordtemp["part"]."';
				arr_url[".$i."]='".html_entity_decode($recordtemp["url"])."';
				arr_desc[".$i."]='".html_entity_decode($recordtemp["description"])."';
			</script>";
		//arr_subject[".$i."]='".$recordtemp["color"]."';
	$i++; 
	}
	echo "<script>window.onload=display;</script>";
}?>
<!-- end script add part of show -->

<div class="JO_left">

<?php
if( $_REQUEST[ "SO_show_key" ] ) {
	$query = "
		SELECT	*
		FROM	`show`
		WHERE	`key` = '" . $_REQUEST[ "SO_show_key" ] . "'
	";
	$recordSet = JO_query( $query );

	if( $recordSet -> num_rows ) $record = $recordSet -> fetch_array( );
}
?>
<script type="text/javascript">
function JO_show_hide_uploadbox(){
	if(document.getElementById("chkupload").checked==true){
		//alert("check fail");
		var ct 	 ="<input type='file' name='ub_file' style='width: 571px;' />";
		ct 		+= "<input type='checkbox' id='chkupload' onchange='JO_show_hide_uploadbox();' checked='checked' style='vertical-align: middle; margin-right: 8px;' /><?=$JO["attach_link"];?>";
		document.getElementById("td_show_hide_uploadbox").innerHTML= ct;
    }
    else{
        //alert("check true");
        var ct 	 = "<input id	= 'txt_image'	type = 'text' name = 'txt_image' value = '<?= $record[ "image" ] ?>'	style = 'width:571px;'/>";
        ct 		+="<input type='checkbox' id='chkupload'	onchange='JO_show_hide_uploadbox();' style='vertical-align: middle; margin-right: 8px;' /><?=$JO["upload_file"];?>";                
    	document.getElementById("td_show_hide_uploadbox").innerHTML= ct;
    }
}
</script>

<!-- Form -->

<form
	action	= "JO_71_SO/SO_05_execution.php"
	enctype	= "multipart/form-data"
	target	= "JO_integration_asynchronous"
	method	= "post"
	onsubmit= "return checkInput(this);"
	class	= "JO_bar_720"
>

	<input type="hidden" name="SO_query"		value="show_<?= empty( $_REQUEST[ "SO_show_key" ] ) ? "insert" : "update" ?>"	/>
	<input type="hidden" name="SO_show_key"		value="<?= $_REQUEST[ "SO_show_key" ] ?>"/>
	<input type="hidden" name="SO_show_array"	value="0" id="SO_show_array"/>

	<h1><?= empty( $_REQUEST[ "SO_show_key" ] ) ? $JO["insert"] : $JO["update"]?></h1>
	
	<div>
	
	<table>
	
		<tr>
			<th style="width: 81px;"><?=$JO["subject"];?></th>
			<td>
				<input
					type	= "text"
					name	= "txt_subject"
					value	= "<?=$record["subject"];?>"
					style	= "width: <?= $JO_MSIE ? 569 : 571 ?>px;"
				/>
			</td>
		</tr>
		
		<tr>
			<th style="width: 81px;"><?=$JO["show"];?></th>
			<td>
				<select name="cbo_channel" style="width: 584px;">
					<option></option> <!-- non publishment -->
					<?php
					$querytemp = "select `key`,`subject` from `show_category` where `delete` IS NULL";
					$recordSettemp = JO_query( $querytemp );
					if( $recordSettemp -> num_rows )
					while( $recordtemp = $recordSettemp -> fetch_array( ) ) {	 
					?>
						<option value="<?=$recordtemp["key"];?>"
							<?php
							if(isset($record["channel"]) && $record["channel"] == $recordtemp["key"])
								echo 'selected="selected"'; 
							?>
						><?=$recordtemp["subject"]?></option>
					<?php }?>
				</select> 
			</td>
		</tr>
		
		<tr>
			<th><?=$JO["episode"];?></th>
			<td>
				<input
					type		= "text"
					name		= "txt_episode"
					value		= "<?=$record["episode"];?>"
					style		= "width: <?= $JO_MSIE ? 569 : 571 ?>px;"
					onkeypress	= "return keypress(event);"
				 />
			</td>
		</tr>

		<tr>
			<th style="padding-top: 12px; vertical-align: top;"><?=$JO["description"];?></th>
			<td>
				<textarea
					name	= "ta_description"
					style	= "width: 567px; height: <?=substr_count( $record[ "description" ], "\r\n" ) ? substr_count( $record[ "description" ], "\r\n" ) * 16 : 16 ?>px;"
					onkeyup	= "this.style.height = ( this.value.split( '\n' ).length * 16 ) + 'px'";"
				><?=$record["description"];?></textarea>

			</td>
		</tr>

		<tr>
			<th style="padding-top: 12px; vertical-align: top;">
				<?=$JO["image"];?>
			</th>
			<td id	= "td_show_hide_uploadbox">
				<input
					id	= "txt_image"	
					type = "text" 
					name = "txt_image" 
					value = "<?= $record[ "image" ] ?>"	
					style = "width: <?= $JO_MSIE ? 569 : 571 ?>px;"
				/>
				<input
					type="checkbox"
				 	id="chkupload"
				 	onchange="JO_show_hide_uploadbox();"
				 	style="vertical-align: middle; margin-right: 8px;"
				/><?=$JO["upload_file"];?>
			</td>
		</tr>

		<tr>
			<th><?=$JO["viewer"];?></th>
			<td>
				<input
					type		= "text"
					name		= "txt_viewer"
					value		= "<?= $record[ "viewer" ] ?>"
					style		= "width: <?= $JO_MSIE ? 569 : 571 ?>px;"
					onkeypress	= "return keypress(event);"
					disabled	= "disabled"
				 />
			</td>
		</tr>

	</table>
	
	<table id="tb_detail">
		<tr>
			<th style="width: 28px;"><?=$JO["part"];		?></th>
			<th						><?=$JO["url"];			?></th>
			<th						><?=$JO["description"];	?></th>
			<th style="width: 28px;vertical-align: middle;text-align: center;"><?=$JO["delete"];?></th>
		</tr>
		<tr>
			<td colspan="5">
				<a href="javascript:addInput()"><?=$JO["add_field"];?></a><br>
			</td>
		</tr>
	</table>
	
	<input
		type	= "submit"
		value	= "<?= empty( $_REQUEST[ "SO_show_key" ] ) ? $JO["insert"] : $JO["update"]?>"
		class	= "JO_button"
		style	= "width: 98%;"
	/>

	</div>

</form>

<!-- List -->

<div class="JO_bar_720">

	<h1>Danh Sách Chương Trình TV</h1>
		
	<div>
		<form id="frm_show" name="frm_show">
		<table style="margin: 2px 3px 1px -3px;">

			<tr>
				<th><?=$JO["subject"];	?></th>
				<th><?=$JO["episode"];	?></th>
				<th><?=$JO["viewer"];	?></th>
				<th><a href="#" onclick="delete_items();"><?=$JO["delete"];	?></a></th>
			</tr>

			<?php
				$query = "
					SELECT		*
					FROM		`show`
					WHERE		`delete` IS NULL
					ORDER BY	`key` DESC
				";

				$recordSet = JO_query( $query );

				if( $recordSet -> num_rows ) while( $record = $recordSet -> fetch_array( ) ) {

			?>
			<tr>

				<td onclick="ShowProduct(<?= $record[ "key" ] ?>);"><?=$record["subject"];	?></td>
				<td onclick="ShowProduct(<?= $record[ "key" ] ?>);"><?=$record["episode"];	?></td>
				<td onclick="ShowProduct(<?= $record[ "key" ] ?>);"><?=$record["viewer"];	?></td>
				<td>
					<input
						type	="checkbox" 
						id		="chk_delete"
						name	="chk_delete"
						value	="<?=$record["key"]?>"
					/>
				</td>

			</tr>
			<? } ?>

		</table>
		</form>
	</div>

</div>
	
</div>
